<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/24
 * Time: 15:26
 */

namespace app\api\model;


use think\Db;
use think\Exception;

class Mini extends BaseModel
{
    protected $hidden=['t_id','create_time','update_time','delete_time'];
    protected $autoWriteTimestamp = true;

    /**
     * 与登录账号一对一关系
     * @return \think\model\relation\HasOne
     */
    public function mini(){
        return $this->hasOne('ThirdApp','mini_id','id');
    }

    /**
     * 小程序授权表一对一关系
     * @return \think\model\relation\HasOne
     */
    public function audit(){
        return $this->hasOne('MiniAudit','mini_id','id');
    }

    /**
     * 小程序商城设置表一对一
     * @return \think\model\relation\HasOne
     */
    public function mall(){
        return $this->hasOne('MallSetup','mini_id','id');
    }
    /**
     * 获取授权信息
     * @param $appid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMini($appid){
        $mini = new Mini();
        $info = $mini->where('authorizer_appid','=',$appid)->find();
        return $info;
    }
    /**
     * 根据用户id获取小程序信息
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMiniByUser($t_id){
        $mini = new Mini();
        $info = $mini->where('t_id','=',$t_id)->cache($t_id.'mini',7200)->find();
        return $info;
    }
    /**
     * 添加小程序授权信息
     * @param $uid
     * @param $info
     * @return $this
     */
    public static function createMini($uid,$info){
        $data= [
            't_id'=>$uid,
            'authorizer_appid'=>$info->authorization_info->authorizer_appid,
            'authorizer_refresh_token'=>preg_replace("/refreshtoken@@@/","",$info->authorization_info->authorizer_refresh_token),
            'func_info'=>json_encode($info->authorization_info->func_info),
            'nick_name'=>$info->authorizer_info->nick_name,
            'head_img'=>$info->authorizer_info->head_img,
            'service_type_info'=>$info->authorizer_info->service_type_info->id,
            'verify_type_info'=>$info->authorizer_info->verify_type_info->id,
            'user_name'=>$info->authorizer_info->user_name,
            'principal_name'=>$info->authorizer_info->principal_name,
            'business_info'=>json_encode($info->authorizer_info->business_info),
            'qrcode_url'=>$info->authorizer_info->qrcode_url,
            'network'=>json_encode($info->authorizer_info->MiniProgramInfo->network),
            'categories'=>json_encode($info->authorizer_info->MiniProgramInfo->categories),
        ];
        Db::startTrans();
        try{
            $mini = self::create($data);
            $mini->audit()->save(['appid' => $data['authorizer_appid']]);//级联新增
            $mall['appid'] = $data['authorizer_appid'];
            $mall['t_id'] = $uid;
            $mini->mall()->save($mall);//级联新增
            $status = ThirdApp::where('id','=',$uid)->update(['mini_id'=>$mini->id]);
            Db::commit();
            return $status;
        }catch (Exception $exception){
            Db::rollback();
            return false;
        }
    }
    /**
     * 用户取消授权后，又继续授权
     * @param $uid
     * @param $info
     * @return false|int
     */
    public static function updateMini($uid,$info){
        $data= [
            't_id'=>$uid,
            'authorizer_appid'=>$info->authorization_info->authorizer_appid,
            'authorizer_refresh_token'=>preg_replace("/refreshtoken@@@/","",$info->authorization_info->authorizer_refresh_token),
            'func_info'=>json_encode($info->authorization_info->func_info),
            'nick_name'=>$info->authorizer_info->nick_name,
            'head_img'=>$info->authorizer_info->head_img,
            'service_type_info'=>$info->authorizer_info->service_type_info->id,
            'verify_type_info'=>$info->authorizer_info->verify_type_info->id,
            'user_name'=>$info->authorizer_info->user_name,
            'principal_name'=>$info->authorizer_info->principal_name,
            'business_info'=>json_encode($info->authorizer_info->business_info),
            'qrcode_url'=>$info->authorizer_info->qrcode_url,
            'network'=>json_encode($info->authorizer_info->MiniProgramInfo->network),
            'category'=>json_encode($info->authorizer_info->MiniProgramInfo->categories),
            'auth_status'=>1
        ];
        $mini = new Mini();
        return $mini->where('authorizer_appid','=',$data['authorizer_appid'])->update($data);
    }
    /**
     * 用户取消小程序授权
     * @param $appid
     * @return false|int
     */
    public static function cancelMini($appid){
        $mini = new Mini();
        $info =  $mini->where('authorizer_appid','=',$appid)->find();
        ThirdApp::where('id','=',$info->t_id)->update(['mini_id'=>0]);//更新用户授权信息
        $status = $mini->where('authorizer_appid','=',$appid)->delete();//删掉小程序授权信息
        MiniAudit::deleteAudit($appid);//删掉小程序审核状态信息
        MallSetup::deleteMall($appid);//删掉商城信息
        return $status;
    }

    /**
     * 获取授权详情
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMiniDetails($t_id){
        $result = self::where('t_id','=',$t_id)->with(['audit'])->find();
        $result->visible(['nick_name','head_img','verify_type_info','principal_name','categories','audit']);
        $result->audit->visible(['user_version']);
        return $result;
    }

}